############################################################
# Summary: This script reproduces the validation step for the sensitivity analysis for the US Presidential Debate Corpus

# Output:
# - Figure 6

# Load data
df <- read_csv("data/results/trump_labeled.csv") |> 
  filter(!is.na(label)) |> 
  filter(model == "deepseak") |> 
  distinct(text, speaker, model, label) |> 
  mutate(
    speaker = recode(speaker, 
                     "harris" = "Kamala Harris",
                     "trump" = "Donald Trump"),
    label = recode(label,
                   "emp" = "Communion",
                   "dur" = "Agency"),
    model = recode(model,
                   "gpt" = "GPT-4o",
                   "deepseak" = "Deepseek-V3")
  )

# Count words per sentence
df$count_words_sentence <- str_count(df$text, '\\w+')

# Compute share and standard error of proportions
df_stats <- df |> 
  filter(label != "none") |> 
  group_by(speaker, model) |> 
  mutate(total = n()) |> 
  ungroup() |> 
  group_by(speaker, model, label) |> 
  summarise(
    count = n(),
    share = count / first(total),
    se = sqrt(share * (1 - share) / first(total)),
    .groups = "drop"
  )

# Define label colors
label_colors <- c("Agency" = "#E67E22", "Communion" = "#3498DB")

# Image paths
image_paths <- tibble(
  speaker = c("Kamala Harris", "Donald Trump"),
  image = c("img/harris.jpg", "img/TrumpPortrait.jpg")
)

# Merge image paths
df_stats <- df_stats |> left_join(image_paths, by = "speaker")

# Plot with error bars
p1 <- ggplot(df_stats, aes(x = model, y = share, fill = label)) +
  geom_bar(stat = "identity", position = position_dodge(width = .9)) +
  geom_errorbar(aes(ymin = share - se, ymax = share + se),
                position = position_dodge(width = .9),
                width = 0.2) +
  scale_fill_manual(values = label_colors) +
  facet_wrap(~speaker) +
  ylim(0, max(df_stats$share + df_stats$se) * 1.2) +
  labs(x = "Cue", y = "Share", fill = "Cue") +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_text(hjust = 1),
    strip.text = element_text(face = "bold")
  )

# Optional: Count plot (unchanged, no error bars)
p2 <- ggplot(df_stats, aes(x = model, y = count, fill = label)) +
  geom_bar(stat = "identity", position = position_dodge(width = .9)) +
  facet_wrap(~speaker) +
  scale_fill_manual(values = label_colors) +
  ylim(0, max(df_stats$count) * 1.2) +
  labs(x = "Speaker", y = "Count", fill = "Cue") +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_text(hjust = 1),
    strip.text = element_text(face = "bold")
  )

# Combine plots (just using p1 here)
p_combined <- ggarrange(p1, common.legend = TRUE, nrow = 1, legend = "bottom")

# Save plot
ggsave("output/Figure_6_shares_Trump_Harris.png", plot = p_combined, width = 10, height = 4, dpi = 800)
